<template id='Register'>
  <div class="loginContainer">
    <h1 class="title">注册</h1>
    <el-page-header @back="goBack" content=""> </el-page-header>
    <img class="headImg" src="~assets/img/login/logo3.png" alt="" />
    <el-form 
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="邮箱" prop="email" >
        <el-input v-model="email"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="nickname">
        <el-input v-model="nickname"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off">
          </el-input>
      </el-form-item>
      <el-form-item>
       <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      nickname: "",
      email: "",
      ruleForm: {
        pass: "",
        checkPass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      if(this.email && this.nickname) {
         let data = {
            email: this.email,
            nickname: this.nickname,
            password: this.ruleForm.pass
          };
          event.preventDefault();
          var result = axios({
            url: "http://localhost:3000/register",
            method: "post",
            data
          }).then(res => {
            let err_code = res.data.err_code;
            if (err_code === 0) {
              this.$toast.show("注册成功", 1000);
              this.$router.push("/login");
            } else if (err_code === 1) {
              this.$toast.show("邮箱已存在", 1000);
            } else if (err_code === 2) {
              this.$toast.show("昵称已存在", 1000);
            } else if (err_code === 500) {
              this.$toast.show("服务器正忙，稍后重试");
            }
          });
      } else {
        this.$toast.show('请输入信息')
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.email = "";
      this.nickname = "";
    },
    goBack() {
      this.$router.go(-1);
    },
    
  }
};
</script>

<style lang="css" scoped>
.loginContainer {
  position: relative;
  width: 100%;
  height: calc(100vh - 44px);
  background-color: #abdde8;
}
.title {
  margin: 0;
  text-align: center;
}
.demo-ruleForm {
  position: absolute;
 
}
.headImg {
  position: relative;
  margin: 10px 0;
  width: 100px;
  left: 50%;
  transform: translateX(-50%);
}

</style>
